package club.pinea.server;

import club.pinea.proto.BasicProto;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//@Component
public class LobbyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private LobbyServerHandler lobbyServerHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //添加心跳检测5秒未读5秒未写，10秒未读写
        ch.pipeline()
                .addLast(new IdleStateHandler(10, 10, 10))
                .addLast(new ProtobufVarint32FrameDecoder())
                //给服务器添加编码解码器
                .addLast(new ProtobufDecoder(BasicProto.BaseMessage.getDefaultInstance()))
                .addLast(new ProtobufVarint32LengthFieldPrepender())
                .addLast(new ProtobufEncoder())
                //添加服务端业务处理类
                .addLast(lobbyServerHandler);
    }
}
